﻿'use strict';


var c=0 ;
var t ;
function timedCount() 
{ 
	$("#txt").val(c);
	c=c+1 ;
	t=setTimeout("timedCount()",1000) ;
} 
function stopCount() 
{ 
	clearTimeout(t) ;
} 

// 写入后台调试信息
function trace(text) 
{
  document.getElementById("msg").innerHTML = text;
}

// 启动WebSocket
var socket;
if(!window.WebSocket)
{
  window.WebSocket = window.MozWebSocket;
}

if(window.WebSocket)
{
//打电话功能，先注释掉
  // 运行前请把192.168.1.82修改成国信呼叫中心所在服务器的IP地址和WebSocket的端口
  socket = new WebSocket("ws://192.168.1.200:7397/websocket");
  socket.onmessage = function(event)
  {
    var json = JSON.parse(event.data);
    
    var id = json.id;
    var caller = json.caller; // 主叫号码
    var callee = json.callee; // 被叫号码
    //alert(id);
    
    var state = json.state;
    if (state == "ringing")
    {
    	var url="customer/callLog/toadd/"+caller+"_"+callee+"_"+encodeURI(id);
    	openTitleLayer("新增来电",url,900,800);
    	
        state = "<font color=\"#9370DB\">正在响铃...</font>";
    }
    else if (state == "talking")
    {
    	//timedCount();
        state = "<font color=\"#6B8E23\">通话中...</font>";
    } 
    else if (state == "hungup")
    {
    	//stopCount();
        state = "<font color=\"#FF7F50\">已挂机</font>";
    }     
    else if (state == "canceled")
    {
        state = "<font color=\"#CD5C5C\">已停止呼叫</font>";
    }
    else if (state == "calling")
    {
        state = "<font color=\"#ADFF2F\">尝试开始呼叫</font>";
    }
    else
    {
        // 未知状态
    }

    var strsdp =  "<BR>呼叫状态: <B>" + state + "</B><BR>主叫号码: "+caller + "<BR>被叫号码: " + callee +  " <BR>会话编号: " +id;
    
    // 收到呼叫中心的呼叫通知消息
  };

  socket.onopen = function(event)
  {
     //trace("打开WebSoket 服务正常，浏览器支持WebSoket!");
  };

  socket.onclose = function(event)
  {
   // trace("WebSocket 关闭");
  };
}
else
{
 // trace("您的浏览器不支持WebSocket协议！");
}

function wsMsgSend(msg)
{
   if(!window.WebSocket){ return; }
   if(socket.readyState == WebSocket.OPEN)
   {
     socket.send(msg);
   }
}

var g_localnum="";

var btnName = "订阅";
var sbuscribeBtn = document.getElementById('sbuscribeBtn');
function doSubscribe()
{
    if (btnName=="订阅")
    {
        btnName="注销";
        sbuscribeBtn.innerHTML = btnName;
        subscribe();
    }
    else
    {
        btnName ="订阅";
        sbuscribeBtn.innerHTML = btnName;
        unsubscribe();
    }
}

// 订阅
function subscribe(g_localnum)
{
 g_localnum = g_localnum;
 var strdata = JSON.stringify
 ({
    "method": "SUBSCRIBE",
    "from": g_localnum,
    "to": g_localnum,
    "expires" : 360000
  });
  //alert(g_localnum);
 wsMsgSend(strdata);
 
}


function unsubscribe()
{
    if (g_localnum != "")
    {
         var strdata = JSON.stringify
         ({
            "method": "SUBSCRIBE",
            "from": g_localnum,
            "to": g_localnum,
            "expires" : 0
          });
          
         wsMsgSend(strdata);
         
    }
}


